import 'package:flutter/material.dart';
import 'package:top_foundation/utils/util.dart';

import 'index.dart';

class DatePickerController extends GetxController {
  DatePickerController();

  DatePickerState state = DatePickerState();

  void back() {
    state.bgColor = Colors.transparent;
    update();

    Get.back();
  }

  void actionBack() {
    state.result(null, state.apm == 0);
    back();
  }

  void actionSure() {
    DateTime curr;
    switch (state.type) {
      case DatePickerType.y:
        curr = DateTime(state.year);
        break;
      case DatePickerType.quarter:
        curr = DateTime(state.year, state.month);
        break;
      case DatePickerType.ym:
        curr = DateTime(state.year, state.month);
        break;
      case DatePickerType.ymd:
        curr = DateTime(state.year, state.month, state.day);
        break;
      case DatePickerType.ymdhm:
        curr = DateTime(
            state.year, state.month, state.day, state.hour, state.minute);
        break;
      case DatePickerType.ymdapm:
        curr = DateTime(
            state.year, state.month, state.day, state.apm == 0 ? 8 : 18);
        break;
      case DatePickerType.meeting:
        curr = DateTime(
            state.year, state.month, state.day, state.hour, state.minute);
        break;
    }

    state.result(curr, state.apm == 0);
    back();
  }

  void ready() {
    // update();
  }

  int getMinQuarter() {
    var q = 1;
    if (state.minDate != null) {
      if (state.year == state.minDate!.year) {
        q = state.minDate!.quarter;
      }
    }

    if (state.quarter < q) {
      state.quarter = q;
    }

    return q;
  }

  int getMaxQuarter() {
    var q = 4;
    if (state.minDate != null) {
      if (state.year == state.maxDate!.year) {
        q = state.maxDate!.quarter;
      }
    }
    if (state.quarter > q) {
      state.quarter = q;
    }
    return q;
  }

  int getMinMonth() {
    var month = 1;
    if (state.minDate != null) {
      if (state.year == state.minDate!.year) {
        month = state.minDate!.month;
      }
    }

    if (state.month < month) {
      state.month = month;
    }

    return month;
  }

  int getMaxMonth() {
    var month = 12;
    if (state.minDate != null) {
      if (state.year == state.maxDate!.year) {
        month = state.maxDate!.month;
      }
    }
    if (state.month > month) {
      state.month = month;
    }
    return month;
  }

  int getMinDay() {
    var x = 1;
    if (state.minDate != null) {
      if (state.year == state.minDate!.year &&
          state.month == state.minDate!.month) {
        x = state.minDate!.day;
      }
    }
    if (state.day < x) {
      state.day = x;
    }
    return x;
  }

  int getMaxDay() {
    var x = DateUtil.getCurrentDays(year: state.year, month: state.month);
    if (state.maxDate != null) {
      if (state.year == state.maxDate!.year &&
          state.month == state.maxDate!.month) {
        x = state.maxDate!.day;
      }
    }
    if (state.day > x) {
      state.day = x;
    }
    return x;
  }

  int getMinHour() {
    var x = 1;
    if (state.minDate != null) {
      if (state.year == state.minDate!.year &&
          state.month == state.minDate!.month &&
          state.day == state.minDate!.day) {
        x = state.minDate!.hour;
      }
    }
    if (state.type == DatePickerType.meeting) {
      if (x < 8) {
        x = 8;
      }
    }

    if (state.hour < x) {
      state.hour = x;
    }

    return x;
  }

  int getMaxHour() {
    var x = 24;
    if (state.maxDate != null) {
      if (state.year == state.maxDate!.year &&
          state.month == state.maxDate!.month &&
          state.day == state.maxDate!.day) {
        x = state.maxDate!.hour;
      }
    }
    if (state.type == DatePickerType.meeting) {
      if (x > 20) {
        x = 20;
      }
    }

    if (state.hour > x) {
      state.hour = x;
    }
    return x;
  }

  int getMinMinute() {
    var x = 0;
    if (state.minDate != null) {
      if (state.year == state.minDate!.year &&
          state.month == state.minDate!.month &&
          state.day == state.minDate!.day &&
          state.hour == state.minDate!.hour) {
        x = state.minDate!.minute;
      }
    }

    if (state.minute < x) {
      state.minute = x;
    }
    return x;
  }

  int getMaxMinute() {
    var x = 60;
    if (state.maxDate != null) {
      if (state.year == state.maxDate!.year &&
          state.month == state.maxDate!.month &&
          state.day == state.maxDate!.day &&
          state.hour == state.maxDate!.hour) {
        x = state.maxDate!.minute;
      }
    }

    if (state.minute > x) {
      state.minute = x;
    }
    return x;
  }

  List<int>? getMeetingData() {
    if (state.type == DatePickerType.meeting) {
      return [0, 30];
    }
    return null;
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  void initData() {
    state.curr ??= DateTime.now();
    var now = state.curr!;

    state.year = now.year;
    state.month = now.month;
    state.day = now.day;
    state.hour = now.hour;
    state.minute = state.type == DatePickerType.meeting ? 0 : now.minute;
    state.quarter = now.quarter;
    if (state.minDate != null) {
      state.curr = now;
    }

    state.minDate ??= now.after(year: -10);
    state.maxDate ??= now.after(year: 10);

    Get.log('${state.minDate}');
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    Future.delayed(const Duration(milliseconds: 200)).then((value) {
      state.bgColor = Colors.black45;
      update();
    });
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
